<?php
namespace app\index\controller;
use think\Controller;
class Index extends Controller
{
    public function index()
    {
        if(!session('blog_user','','blog')){
            $this->error('请您先登录',url('index/user/login'));
        }
        //获取到登录用户的值
        $user = session('blog_user','','blog');
        $username = $user['username'];
        $dataConditions = input('get.');
        $dataas=[];

        if(!empty($dataConditions['title'])){
            $dataas =[
                ['title|content','like','%'.$dataConditions['title'].'%'],

                ];

        }
        $data= model('Releases')->dataByStatus($dataas);
        //设置默认图片路径
        //$imgpath = "__static__/img/index/default.jpeg";
        //遍历数据修改logimg字段 1优先上传图片 2.内容图片  3。默认给一个图片
        foreach($data as $k=>$val){
            if(!empty($val['logimg'])){
                //echo '开始 ';
                $val['logimg'];
            }elseif(!empty(getContentImg($val['content']))){
                //echo ' 中间 ';
                $val['logimg'] = getContentImg($val['content']);
            }else{
                //echo '最后 ';
                $val['logimg'] = "/static/img/index/default.jpeg";
            }
        }

        //作者  通过user_id获取用户名
        foreach($data as $k=>$val){
            if(!empty($val['user_id'])){//echo '开始 ';
                $userid = $val['user_id'];
                $rel = model('User')->usernameById($userid)->toArray();
                $val['user_id'] = $rel[0]['username'];
            }else{
                $val['user_id'] = "不存在";
            }
            //var_dump($val['user_id']);
        }



        return $this->fetch('',[
            'data'=>$data,
            'dataConditions' =>empty($dataConditions['title']) ? '' : $dataConditions['title'],
            'username' => $username,

        ]);
    }



}
